//
// Created by Administrator on 2025/3/13.
//
#include <algorithm>
#include<iostream>
#include <thread>
#include <vector>
using namespace std;
#include <unistd.h>

#include "mem.h"
#define MYNAME  "昂撒"
#define DEBUG
#define DEBUG_VERSION 2.1
#define CONCAT(a,b) a##b



class Box
{
    public:
    Box() {
        cout << "Box init" <<endl;
    }
    ~Box() {
        cout << "Box destroy" <<endl;
    }
};

// class Box
// {
// public:
//     Box() {
//         cout << "调用构造函数！" <<endl;
//     }
//     ~Box() {
//         cout << "调用析构函数！" <<endl;
//     }
// };


int mem1() {
    int *array = new int[10];
    for (int i = 0; i < 10; i++) {
        array[i] = i;
    }
    delete [] array;
    return 0;
}

int mem2() {
    int a1_length = 20;
    int a2_length = 10;
    int **array ;
    array = new int *[a1_length];
    for (int i = 0; i < a1_length; i++) {
        array[i] = new int[a2_length];
        for (int j = 0; j < a2_length; j++) {
            array[i][j] = i;
        }
        delete [] array[i];
    };
    delete [] array;
    return 0;
}

int mem3() {
    int a1_length = 20;
    int a2_length = 10;
    int a3_length = 5;
    int ***array ;
    array = new int **[a1_length];
    for (int i = 0; i < a1_length; i++) {
        array[i] = new int*[a2_length];
        for (int j = 0; j < a2_length; j++) {
            array[i][j] = new int[a3_length];
            for (int k = 0; k < a3_length; k++) {
                array[i][j][k] = i;
            }
            delete [] array[i][j];
        }
        delete [] array[i];
    };
    delete [] array;
    return 0;
}

int mem4() {
    Box *array = new Box[10];
    // Box *array;
    // for (int i = 0; i < 10; i++) {
    //     array[i] = Box();
    // }
    delete [] array;
    return 0;
}

int test_pre() {
    int xy = 1000;
    cout << "show concat "<< CONCAT(x,y) << endl;
    cout << "show place-text : " <<MYNAME <<endl;



#ifdef DEBUG
    cout << "version : " << DEBUG_VERSION << endl;

#endif
    cout << "name" <<endl;

    cout << "version is not defined !" << endl;
    cout <<  "return repr "<<endl;
    return 0;
}

void signal_handle(int sign_num) {
    cout<< "signal_handle is called !" << endl;
    exit(0);
}

int test_sign() {
    signal(SIGINT,signal_handle);
    int num= 0;
    while(1) {
        cout<<"signal test"<<endl;
        num ++;
        sleep(1);
        if (num == 10) {
            raise(SIGINT);
        }
    }
    return 0;
}

void test_ref(int num) {
    for(int i=0;i<10;i++) {
        num ++;
    }
    // num ++;
    cout<< num <<endl;

}

class Test_thread {
    public:
        void operator()(int num){
            int local_num = num;
            for(int i=0;i<10;i++) {
                num ++;
            }
            cout<< "thread "<< local_num << "; res :"<< num <<endl;
        }
};


void test_thread() {
    int num = 0;
    std::thread t1(test_ref, std::ref(num) );
    t1.join();
    cout<<"test thread :"<< num<<endl;


    num = 10;
    std::thread t2(Test_thread(),std::ref(num) );
    t2.join();

}

int random_num() {
    srand(time(nullptr));
    return rand();
}

void thread_fun(int num) {
    thread_local int thread_id = num;
    while(num-- > 0) {
        cout<<"thread thread_id :" <<thread_id << "; res : "<< num <<endl;
    }
}

thread gen_thread(int num) {
    thread t(thread_fun,num);
    return t;
}

void test_thread_fun() {
    int num = 10;
    vector<thread> threads_list;
    for(int i=0;i < num;i++) {
        int num = random_num();

        threads_list.push_back(thread(thread_fun,num));
    };
    for (int i = 0; i < threads_list.size(); i++) {
        threads_list[i].join();
    }
    cout<<"thread fun over" <<endl;
}




int main() {
    // mem1();
    // mem2();
    // mem4();
    // test_pre();
    // test_sign();
    // test_thread();
    test_thread_fun();
}